﻿using Newtonsoft.Json;
using Quartz;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace LD.Quartz
{
    public class DeleteDBJob : IJob
    {
        Task IJob.Execute(IJobExecutionContext context)
        {
            return DeleteData();
        }
        public Task DeleteData()
        {
            return Task.Run(() =>
            {
                Dictionary<string, string> data = Cache.GetCache("ChartParam") != null ?
                        JsonConvert.DeserializeObject<Dictionary<string, string>>(Cache.GetCache("ChartParam")?.ToString()) : null;
                if (data != null)
                {
                    if (int.TryParse(data["SaveDay"], out var day))
                    {
                        DateTime dateTime = DateTime.Now.Date.AddDays(-day);

                        var result = DBHelper.Instance.Sql.Deleteable<TB_Product>()
                                .Where(p => p.StartTime < dateTime)
                                .ExecuteCommand();

                        result = DBHelper.Instance.Sql.Deleteable<TB_Alarm>()
                              .Where(p => p.CreateTime < dateTime)
                              .ExecuteCommand();

                        result = DBHelper.Instance.Sql.Deleteable<TB_Screw>()
                          .Where(p => p.CreateTime < dateTime)
                          .ExecuteCommand();

                        var list = DBHelper.Instance.Sql.Ado.SqlQuery<int>("VACUUM");
                    }
                }
            });
        }
    }

}
